package edu.neu.agv_backend.controller;

import edu.neu.agv_backend.dto.*;
import edu.neu.agv_backend.service.TaskService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/agv/task")
public class TaskController {

    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @GetMapping("/list")
    public TableDataInfo<AgvTaskDto> listTask(@RequestParam Map<String, String> params) {
        String query = params.isEmpty() ? "" : ("?" + params.entrySet().stream()
                .map(e -> e.getKey() + "=" + e.getValue())
                .collect(Collectors.joining("&")));
        return taskService.listTask(query);
    }

    @GetMapping("/{id}")
    public AjaxResult<AgvTaskDto> getTask(@PathVariable Long id) {
        return taskService.getTask(id);
    }

    @PostMapping
    public AjaxResult<Void> addTask(@RequestBody AgvTaskDto dto) {
        return taskService.addTask(dto);
    }

    @PutMapping
    public AjaxResult<Void> updateTask(@RequestBody AgvTaskDto dto) {
        return taskService.updateTask(dto);
    }

    @DeleteMapping("/{id}")
    public AjaxResult<Void> delTask(@PathVariable Long id) {
        return taskService.delTask(id);
    }

    @PostMapping("/start/{id}")
    public AjaxResult<Void> startTask(@PathVariable Long id) {
        return taskService.startTask(id);
    }

    @PostMapping("/end/{id}")
    public AjaxResult<Void> endTask(@PathVariable Long id, @RequestParam(defaultValue = "false") boolean isAbort) {
        return taskService.endTask(id, isAbort);
    }

    @GetMapping("/preupload/{id}")
    public AjaxResult<Void> preUploadTask(@PathVariable Long id) {
        return taskService.preUpload(id);
    }

    @PostMapping("/upload/{id}")
    public AjaxResult<Void> uploadTask(@PathVariable Long id) {
        return taskService.upload(id);
    }
} 